package cn.zbmao.web.controller.user;

import cn.zbmao.web.bean.User;
import cn.zbmao.web.interceptor.user.UserIntercepter;
import cn.zbmao.web.util.dao.DaoFactory;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.JsonRender;

import java.util.List;

public class UserUrlController extends Controller {

	public void index(){
		User user = (User)getSessionAttr("user");
		List<Record>list = DaoFactory.getUserDao().getUser_url(user.getInt("id"));
		List<Record>url_list = DaoFactory.getUserDao().getUser_url_un(user.getInt("id"));
		setAttr("list", list);
		setAttr("url_list", url_list);
		render("/view/user_url.html");
	}
	
	@Before(UserIntercepter.class)
	public void add(){
		int key = getParaToInt("key");
		User user = (User)getSessionAttr("user");
		if (DaoFactory.getUserDao().findUser_url(key, user.getInt("id"))) {
			setAttr("msg", 0);
		}else{
			DaoFactory.getUserDao().addUser_url(key, user.getInt("id"));
			setAttr("msg", 1);
		}
		render(new JsonRender().forIE());
	}
	
	@Before(UserIntercepter.class)
	public void delete(){
		DaoFactory.getUserDao().deleteUser_url(getParaToInt());
		redirect("/user_url");
	}




}
